export const addTargetForAnchor = (html: string) => {
  if (!html) return html
  if (typeof window === 'undefined' || typeof DOMParser === 'undefined') {
    return html
  }
  const parser = new DOMParser()
  const doc = parser.parseFromString(html, 'text/html')
  const anchors = doc.querySelectorAll('a')
  anchors.forEach((anchor) => {
    anchor.setAttribute('target', '_blank')
    if (!anchor.getAttribute('rel')) {
      anchor.setAttribute('rel', 'noopener noreferrer')
    }
  })
  return doc.body.innerHTML
}
